package jjn.carl.greedy;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author Jjn
 * @since 2023/8/2 09:43
 */
public class LeetCode435 {
    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, (i1, i2) -> {
            if (i1[0] == i2[0]) {
                return Integer.compare(i1[1], i2[1]);
            }
            return Integer.compare(i1[0], i2[0]);
        });
        int count = 0;
        int end = intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] >= end) {
                end = intervals[i][1];
                continue;
            }
            end = Math.min(end, intervals[i][1]);
            count++;
        }
        return count;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int total = scanner.nextInt();
        int[][] intervals = new int[total][2];
        for (int i = 0; i < total; i++) {
            for (int j = 0; j < 2; j++) {
                intervals[i][j] = scanner.nextInt();
            }
        }
        int count = new LeetCode435().eraseOverlapIntervals(intervals);
        System.out.println(count);
    }
}
